package com.yc.boottxx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.boottxx.entity.Car;
import com.yc.boottxx.entity.Image;
import com.yc.boottxx.mapper.CarMapper;
import com.yc.boottxx.mapper.ImageMapper;
import com.yc.boottxx.service.BizException;
import com.yc.boottxx.service.ICarService;
import com.yc.boottxx.service.IImageService;
import com.yc.boottxx.util.Utils;
import org.example.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 天下行租车-车辆表-10 服务实现类
 * </p>
 *
 * @author xl
 * @since 2024-05-07
 */
@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements ICarService {

    @Autowired
    IImageService imageService;
    int cid;

    @Autowired
    CarMapper carMapper;

    @Autowired
    ImageMapper imageMapper;


    @Override
    public int Addcar(Car car,String imagePath) throws BizException {
        LambdaQueryWrapper<Car> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Car::getModel,car.getModel());
        if(count(lqw)>0){
            throw new BizException("该车型已经在库","model");
        }
        this.save(car);
        cid = car.getId();
        addCarImage(imagePath);
        System.out.println("自增id是" + car.getId());
        return car.getId();
    }
    public Result addCarImage(String imagePath) throws BizException {
        if ( cid > 0 ){
            Image images = new Image();
            images.setCid(cid);
            String[] img = imagePath.split(",");
            System.out.println("img集合长度为:"+img.length);
            for (int i = 0; i < img.length ; i++) {
                System.err.println("图片名称为："+img[i]);
            }
            for (int i=0 ;i < img.length ;i++){
                images.setId(null);
                images.setPath(img[i]);
                imageService.addCarImage(cid,images);
            }
            return Result.ok("添加图片成功!");
        }
        return Result.fail("添加图片失败");
    }
}
